package cn.zhz.controller;

import cn.zhz.pojo.Constants;
import cn.zhz.pojo.PageSupport;
import cn.zhz.pojo.Provider;
import cn.zhz.pojo.User;
import cn.zhz.service.provide.ProviderService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/provider")
public class ProviderController extends BaseController{

    private static Logger logger=Logger.getLogger(ProviderController.class);
    @Resource
    ProviderService providerService;

    @RequestMapping(value ="/providerlist.html")
    public String proList(Model model, @RequestParam(value = "queryProCode",required = false)String queryProCode,
                          @RequestParam(value = "queryProName",required = false)String queryProName,
                          @RequestParam(value = "pageIndex",required = false)String pageIndex){
        List<Provider> providerList=null;
        int pageSize= Constants.pageSize;
        int currentPageNo=1;

        if (queryProCode==null){
            queryProCode="";
        }

        if (queryProName==null){
            queryProName="";
        }

        if (pageIndex !=null){
            try {
                currentPageNo=Integer.valueOf(pageIndex);
            }catch (NumberFormatException e){
                return "redirect:/user/syserror.html";
            }
        }
        int totalCount	= providerService.proCount(queryProCode,queryProName);
        //总页数
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页
        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }
        providerList = providerService.getProList(queryProCode,queryProName,currentPageNo,pageSize);
        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProName", queryProName);
        model.addAttribute("queryProCode", queryProCode);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("param",pages);
        return "providerlist";
    }

    @RequestMapping(value = "/providerlist1.html")
    public String list(Model model){
        List<Provider> list=providerService.list();
        model.addAttribute("providerList",list);
        return "providerlist";
    }

    @RequestMapping(value = "/addPro.html")
    public String addPro(@ModelAttribute("provider")Provider provider){
        return "provideradd";
    }

    @RequestMapping(value = "/addprovidersave.html",method = RequestMethod.POST)
    public String add(Provider provider, HttpSession session){
        provider.setCreatedBy(1);
        provider.setCreationDate(new Date());
        if (providerService.addProvide(provider)>0){
            return "redirect:/provider/providerlist.html";
        }
        return "provideradd";
    }

    @RequestMapping(value = "/proview/{id}",method = RequestMethod.GET)
    public String proview(@PathVariable String id,Model model){
        Provider provider=providerService.ProView(id);
        model.addAttribute("provider",provider);
        return "providerview";
    }

    @RequestMapping(value = "/providermodify.html",method = RequestMethod.GET)
    public String getProById(@RequestParam String proid,Model model){
        Provider provider=providerService.ProView(proid);
        model.addAttribute("provider",provider);
        return "providermodify";
    }

    @RequestMapping(value = "/providermodifysave.html",method = RequestMethod.POST)
    public String save(Provider provider){
        logger.debug("供应商id=========>"+provider.getId());
        provider.setModifyBy(1);
        provider.setModifyDate(new Date());
        if (providerService.modifPro(provider)>0){
            return "redirect:/provider/providerlist.html";
        }
        return "provideradd";
    }

    @RequestMapping(value = "/add.html", method = RequestMethod.GET)
    public String addProvider(@ModelAttribute("provider") Provider provider) {
        return "/provider/provideradd";
    }

    @RequestMapping(value = "/addprovidersave.html",method = RequestMethod.POST)
    public String add(Provider provider, HttpSession session,HttpServletRequest request, @RequestParam(value = "attachs",required = false)MultipartFile[] attachs){
        //provider.setCreatedBy(1);
        String idPicPath=null;
        String workPicPath=null;
        String errorInfo=null;
        boolean flag=true;
        String path=request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
        logger.info("uploadFile path=====>"+path);
        if(flag){
            provider.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
            provider.setCreationDate(new Date());
            provider.setCompanyLicPicPath(idPicPath);
            provider.setOrgCodePicPath(workPicPath);
            if(providerService.addProvide(provider)>0){
                return "redirect:/provider/providerlist.html";
            }
        }
        return "provideradd";
    }
}
